<template>
  <basic-container>
    <avue-crud
      :option="option"
      :data="data"
      ref="crud"
      v-model="form"
      :permission="permissionList"
      :before-open="beforeOpen"
      @row-del="rowDel"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button
          type="primary"
          size="small"
          icon="el-icon-plus"
          plain
          @click="dialogVisible = true"
        >添加章节</el-button>
        <el-button type="primary" size="small" icon="el-icon-plus" plain @click="handleHourse">新增课时</el-button>
      </template>

      <template slot-scope="scope" slot="menu">
        <el-button
          icon="el-icon-view"
          size="small"
          class="el-button--text"
          @click="viewVedio(scope.row)"
        >预览</el-button>
      </template>
    </avue-crud>
    <!-- 新增章节 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <div>
        <el-input placeholder="请输入章节编号" v-model="Numbers"></el-input>
        <div style="height:20px;width:100%"></div>
        <el-input placeholder="请输入章节名称" v-model="names"></el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleadd">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 预览视频 -->
    <el-dialog title="提示" :visible.sync="dialogVisible1" width="30%">
      <div>
        <video :src="this.videoUrl" controls="controls" style="width:100%;"></video>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">关闭</el-button>
        <!-- <el-button type="primary" @click="handleadd">确 定</el-button> -->
      </span>
    </el-dialog>

    <!-- 新增课时 -->
    <el-dialog title="提示" :visible.sync="dialogVisible2" width="30%">
      <div>
        <el-form ref="form" :model="formadd" label-width="100px">
          <el-form-item label="课时名称">
            <el-input v-model="formadd.name"></el-input>
          </el-form-item>
          <el-form-item label="是否免费">
            <el-switch v-model="formadd.isFree"></el-switch>
          </el-form-item>
          <el-form-item label="视频时长(分)">
            <el-input v-model="formadd.times"></el-input>
          </el-form-item>
          <el-form-item>
            <el-upload
              class="upload-demo"
              action="api/updateFile/uploadFileVideo"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
              :headers="headers"
              :limit="1"
              :data="submitData"
              :http-request="submitVedio"
            >
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible2=false">关闭</el-button>
        <el-button type="primary" @click="handleAddVideo">确 定</el-button>
      </span>
    </el-dialog>
  </basic-container>
</template>
<script>
import {
  courseTree,
  addDetail,
  addvideo,
  removeDetail,
  selectDetail,
  addVideos
} from "@/api/course/courseDetail";
import { mapGetters } from "vuex";
import iconList from "@/config/iconList";
import { getToken } from "@/util/auth";

export default {
  data() {
    return {
      form: {},
      names: "",
      courseIds: "",
      Numbers: "",
      headers: {
        Authorization: `Basic c3dvcmQ6c3dvcmRfc2VjcmV0`,
        "Blade-Auth": "bearer " + getToken(),
        "Content-Type": "multipart/form-data"
      },
      selectArr: [],
      dialogVisible: false,
      dialogVisible1: false,
      dialogVisible2: false,
      selectionList: [],
      currentSelectId: "",
      videoUrl: "",
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      submitData: {
        vidietype: 1
      },
      formadd: {
        name: "",
        isFree: false,
        times: ""
      },
      option: {
        tip: false,
        dialogWidth: "60%",
        tree: true,
        border: true,
        index: true,
        selection: true,
        addBtn: false,
        column: [
          {
            label: "章节",
            prop: "title",
            search: false,
            rules: [
              {
                required: true,
                message: "请输入章节名称",
                trigger: "blur"
              }
            ]
          },
          {
            label: "名称",
            prop: "name",
            search: false
          },
          {
            label: "课时",
            prop: "id",
            type: "tree",
            dicUrl: "/api/blade-system/menu/tree",
            hide: true,
            props: {
              label: "title"
            },
            rules: [
              {
                required: false,
                message: "请选择所属章节",
                trigger: "click"
              }
            ]
          }
        ]
      },
      data: []
    };
  },
  created() {
    let row = this.$route.query.row;
    this.courseIds = row.id;
    courseTree(row.id).then(res => {
      this.data = res.data.data;
    });
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      // return {
      //   addBtn: this.vaildData(this.permission.menu_add, false),
      //   viewBtn: this.vaildData(this.permission.menu_view, false),
      //   delBtn: this.vaildData(this.permission.menu_delete, false),
      //   editBtn: this.vaildData(this.permission.menu_edit, false)
      // };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
        console.log(ele);
      });
      return ids.join(",");
    }
  },
  methods: {
    // 添加章节
    handleadd() {
      if (this.names == "" || this.Numbers == "") {
        this.$message({
          type: "error",
          message: "输入项不能为空!"
        });
      } else {
        this.dialogVisible = false;
        let obj = {};
        obj.title = this.Numbers;
        obj.courseId = this.courseIds;
        obj.name = this.names;

        addDetail(obj).then(
          () => {
            // loading();
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          },
          error => {
            done();
            console.log(error);
          }
        );
      }
    },
    // 新增课时
    handleHourse() {
      if (this.selectionList.length == 0) {
        this.$message({
          type: "error",
          message: "请选择章节!"
        });
      } else if (this.selectionList.length > 1) {
        this.$message({
          type: "error",
          message: "请选择某一个章节!"
        });
      } else {
        this.dialogVisible2 = true;
        this.currentSelectId = this.selectionList[
          this.selectionList.length - 1
        ].parentId;
      }
    },
    handleAvatarSuccess() {},
    beforeAvatarUpload() {},
    submitVedio(content) {
      let formData = new FormData();
      console.log(content.file);
      formData.append("file", content.file);
      formData.append("vidietype", 1);
      console.log(formData);
      addVideos(formData).then(res => {
        console.log(res);
        console.log("上传视频");
      });
    },

    handleAddVideo() {
      if (this.formadd.name == "" || this.formadd.times == "") {
        this.$message({
          type: "error",
          message: "请检查输入项!"
        });
      } else {
        let obj = {};
        obj.title = this.formadd.name;
        obj.courseId = this.courseIds;
        obj.parentId = this.currentSelectId;
        obj.name = this.formadd.name;
        obj.isFree = this.formadd.isFree;

        addDetail(obj).then(
          () => {
            // loading();
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          },
          error => {
            done();
            console.log(error);
          }
        );
      }
    },
    handleRemove() {},
    // 关闭视频
    closeVideo() {
      this.dialogVisible1 = false;
      this.$refs.video.pause();
    },
    viewVedio(row) {
      if (row.parentId == 0) {
        this.$message({
          type: "error",
          message: "请选择具体章节视频!"
        });
      } else {
        this.dialogVisible1 = true;
        selectDetail(row.id).then(res => {
          console.log("打印视频");
          this.videoUrl = res.data.data.url;
          console.log(res);
        });
      }

      // console.log(row);
    },
    rowSave(row, loading, done) {},
    rowUpdate(row, index, loading, done) {
      addvideo(row).then(
        () => {
          // loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        },
        error => {
          done();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return removeDetail(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    searchReset() {
      this.onLoad(this.page);
    },
    searchChange(params) {
      this.onLoad(this.page, params);
    },
    selectionChange(list) {
      console.log(list);
      this.selectionList = list;
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getMenu(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      courseTree(page.currentPage, page.pageSize, params).then(res => {
        const data = res.data.data;
        this.data = data;
      });
    }
  }
};
</script>
<style>
</style>
